package com.example.drive_safe;

import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener {
	
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);
        SMSManager.context = this;
        Preference prefMethod = getPreferenceScreen().findPreference("prefMethod");
        
        prefMethod.setOnPreferenceChangeListener(prefCheckListener);
        
        Preference schedule_addnew = (Preference) findPreference("schedule_addnew");
        schedule_addnew.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                     public boolean onPreferenceClick(Preference preference) {
                    	 Intent i = new Intent();

    					 // mimeType will popup the chooser any  for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
    					 i.setType("vnd.android.cursor.item/event"); 

    					 // the time when the event should start in miles. This example uses now as the start time and ends in 1 hour
    					 i.putExtra("title","Driving");
    					 i.putExtra("beginTime", new Date().getTime()); 
    					 i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

    					 // the action
    					 i.setAction(Intent.ACTION_EDIT);
    					 startActivity(i);
                    	 return true;
                     }
                 });
    }
    
    Preference.OnPreferenceChangeListener prefCheckListener = new OnPreferenceChangeListener() {

		@Override
		// This method check the state of the Preference that user has set by (e.g. user can set his preference from scheduler, GPS or both)
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			boolean isEventDriving = false;
			if (newValue != null && newValue.equals("Scheduler")) {
				Toast.makeText(SettingsActivity.this, "Scehduler selected",
						Toast.LENGTH_SHORT).show();
				
				checkDrivingEvents();	
				isEventDriving =  true;
			}
			else if(newValue != null && newValue.equals("GPS")){
			Toast.makeText(SettingsActivity.this, "GPS selected",
					Toast.LENGTH_SHORT).show();
			isEventDriving =  true;
			}else if(newValue != null && newValue.equals("Both")){
				Toast.makeText(SettingsActivity.this, "GPS and Scehduler selected",
						Toast.LENGTH_SHORT).show();
					checkDrivingEvents();	
				isEventDriving =  true;
			}
			
			return isEventDriving;
		}

		
		// Check for the new driving event. 
		// If no event is available it asks the user, if they want to schedule a new event? 
		public void checkDrivingEvents() {
			
			if(!SMSManager.checkScheduler()){
				Log.d("Block and Scheduler","False");
				
				AlertDialog.Builder alert = new AlertDialog.Builder(SMSManager.context);

				alert.setTitle("Create Driving Event");
				alert.setMessage("\nNo current Driving event scheduled. \n\nCreate a Driving event?\n");

				 

				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				 
					// all version of android
					 Intent i = new Intent();

					 // mimeType will popup the chooser any  for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
					 i.setType("vnd.android.cursor.item/event"); 

					 // the time the event should start in miles. This example uses now as the start time and ends in 1 hour
					 i.putExtra("title","Driving");
					 i.putExtra("beginTime", new Date().getTime()); 
					 i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

					 // the action
					 i.setAction(Intent.ACTION_EDIT);
					 startActivity(i);
				  }
				});

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});

				alert.show();
			
		}
		}
	};

	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		return false;
	}
    

}
